import { defineStore } from 'pinia'
import { ref, computed } from 'vue'


export const useTokenStore = defineStore('user',()=>{
    
    let id = localStorage.getItem('id')? ref(localStorage.getItem('id')) : ref(0);
    let nickname = localStorage.getItem('nickname')? ref(localStorage.getItem('nickname')) : ref('');
    let email = localStorage.getItem('email')? ref(localStorage.getItem('email')) : ref('');
    let Authorization = localStorage.getItem('Authorization')? ref(localStorage.getItem('Authorization')) : ref('');

    let token;

    let topicId = ref(-1);

    let wechat_nickname = ref('');
    let openid = ref('');


    function login(data){
        id.value = data.id;
        nickname.value = data.nickname;
        email.value = data.email;
        Authorization.value = data.Authorization;
        console.log('save');
        localStorage.setItem('id',JSON.stringify(id.value));
        localStorage.setItem('nickname',nickname.value);
        localStorage.setItem('email',email.value);
        localStorage.setItem('Authorization',Authorization.value);
        console.log("存储");
    }

    function logout(){
        localStorage.clear();
        id.value = 0;
        nickname.value = '';
        email.value = '';
        Authorization.value = '';
        openid.value = '';
    }

    function updateUserInfo(data){
        nickname.value = data.nickname;
        token.value = data.token;
    }


    return {
        id,nickname,email,Authorization,token,topicId,wechat_nickname,openid,login,logout,updateUserInfo
    };
})